$(function () {

    let rst = /^[a-z0-9_-]\w{3,16}$/
    let rut = /^[A-z]\w{5,11}$/
    let numb = /^[0-9]{11}$/

    let str2 = '123456789qwertyuiopasdfghjklzxcvbnmASDFGHJKLZXCVBNMQWERTYUIOP';
    let len = str2.length;
    let yzm;
    let str1;
    for (let i = 1; i <= 4; i++) {
        yzm = Math.floor(Math.random() * (len - 0 + 1) + 0);
        // str1+=`${str.yzm}`
        str2.charAt(yzm)

        str1 = `${str2.charAt(yzm)}` + `${str1}`
    }
    $('.get-yzm').text(str1.substr(1, 4))

    $('.get-yzm').on('click',function(){

        for (let i = 1; i <= 4; i++) {
            yzm = Math.floor(Math.random() * (len - 0 + 1) + 0);
            // str1+=`${str.yzm}`
            str2.charAt(yzm)
    
            str1 = `${str2.charAt(yzm)}` + `${str1}`
        }
        $('.get-yzm').text(str1.substr(1, 4))

    })


    //同意协议
    $('.xy input').on('click',function(){

        let xy = $('.xy input').prop('checked')
       
        if (xy == false) {
            $('.m-2 button').removeClass('clic')
        }
        else {
            $('.m-2 button').addClass('clic')
        }

    })

    // 验证
    $('.m-2 button').on('click', function () {

        //验证账号
        let userid = $('.user-id input').val()
        if (rst.test(userid) == false) {
            $('.id-p').text('账号格式错误！')
            return false;
        }
        else {
            $('.id-p').text('')
        }

        // 验证密码
        let userpd = $('.user-pd input').val()
        if (rut.test(userpd) == false) {
            $('.pd-p').text('密码格式错误！')
            return false;
        }
        else {
            $('.pd-p').text('')

        }

        // 两次密码一致
        let pdok = $('.pd-ok input').val()
        if (pdok != userpd) {
            $('.pdok-p').text('两次密码不一致！')
            return false;
        }
        else {
            $('.pdok-p').text('')
        }

        // 验证手机号格式
        let phone = $('.phone input').val()
        if (numb.test(phone) == false) {
            $('.phone-p').text('手机号格式错误！')
            return false;
        }
        else {
            $('.phone-p').text('')

        }

        // 验证码
        let yzm = $('.yzm input').val()
        let gyzm = $('.get-yzm').text()
        console.log(gyzm)
        if (yzm != gyzm) {
            $('.yzm-p').text('验证码错误！')
            return false;
        }
        else {
            $('.yzm-p').text('')
        }
        
        // 
        let rgb=$('.m-2 button').css('background-color');
        if(rgb=='rgb(166, 166, 166)'){
            return false;
        }
       
        
        window.location.href = './loading.html'
    })


    // 手机验证码
    console.log($('.get-pyzm').text())

    $('.get-pyzm').on('click', function () {

        if ($('.get-pyzm').text() == '获取验证码') {
            let n = 5;
            let timeid = setInterval(function () {

                n--;
                $('.get-pyzm').text(n)
                if (n <= 0) {
                    clearInterval(timeid)
                    $('.get-pyzm').text('获取验证码')
                }

            }, 1000)
        }

    })



})